const db = require('../dbbase/db');
const getData = require("../util/data");
const moment = require('moment');
//获取生产信息
exports.getProductInfo = (req, res, next) => {
    const startTime = getData.getCurrentDayStart();
    const endTime = getData.getCurrentDayEnd();
    const sql = "select t.ihour, sum(t.theroynum) llcl, sum(t.dealnum) sjcl "
        + "from lp_scenc_bate t "
        + "join mt_groupinfo g "
        + "on t.groupid = g.id "
        + "where IHOUR BETWEEN TO_DATE('" + startTime + "','yyyy-mm-dd hh24:mi:ss') "
        + "and TO_DATE('" + endTime + "','yyyy-mm-dd hh24:mi:ss')"
        + "and g.DEPTID = '9' "
        + "group by t.ihour "
        + "order by ihour "
    const params = [];
    db.query(sql, params).then((result) => {
        for (let i = 0; i < result.length; i++) {
            result[i].IHOUR = moment(result[i].IHOUR).format('HH:mm');
        }
        res.status(200).json(result)
    }).catch((err) => {
        console.log(err);
    });
}
//获取生产异常状态信息
exports.getProductStateInfo = (req, res, next) => {
    try {
        startDate = getData.getCurrentDayStart();
        endDate = getData.getCurrentDayEnd();
        const sql = "select m.mtname,s.startname,s.STARTCODE,t.TOUCHTIME,t.ENDTIME,t.STATUS from LP_SCENC_INFO t "
            + "join mt_baseinfo m on t.mtid = m.id "
            + "join LP_SCENC_CONFIG s on s.id = t.scencsetid "
            + "where (endtime >= to_date('" + startDate + "', 'yyyy-mm-dd hh24:mi:ss') or endtime is null) and touchtime < to_date('" + endDate + "', 'yyyy-mm-dd hh24:mi:ss') "
            + "and m.deptid='9' "
            + "and MTTYPE='JJSB' "
            + "ORDER BY TOUCHTIME DESC "
        const params = [];
        db.query(sql, params).then((result) => {
            let totleTime = '';
            for (let i = 0; i < result.length; i++) {
                if(result[i].ENDTIME === null){
                    result[i].ENDTIME = moment().format('YYYY-MM-DD HH:mm:ss');
                }else{
                    result[i].ENDTIME = moment(result[i].ENDTIME).format('YYYY-MM-DD HH:mm:ss');
                }
                result[i].TOUCHTIME = moment(result[i].TOUCHTIME).format('YYYY-MM-DD HH:mm:ss');
               
                let time = new Date(result[i].ENDTIME) - new Date(result[i].TOUCHTIME);
                result[i].totleTime = parseInt( Math.abs(time)/1000/60);
            }
            const deviceList = [];
            const planList = [];
            const changeList = [];
            const qulityList = [];
            const changeKnifeList = [];
            const matterList = [];
            for (let i = 0; i < result.length; i++) {
                if (result[i].STARTNAME === "设备故障") {
                    deviceList.push(result[i])
                }
                if (result[i].STARTNAME === "计划停机") {
                    planList.push(result[i])
                }
                if (result[i].STARTNAME === "更换工装") {
                    changeList.push(result[i])
                }
                if (result[i].STARTNAME === "质量调整") {
                    qulityList.push(result[i])
                }
                if (result[i].STARTNAME === "更换刀具") {
                    changeKnifeList.push(result[i])
                }
                if (result[i].STARTNAME === "物料等待") {
                    matterList.push(result[i])
                }
            }
            let deviceListTotleTime = 0;
            let planListTotleTime = 0;
            let changeListTime = 0;
            let qulityListTime = 0;
            let changeKnifeListTime = 0;
            let matterListTime = 0;
            for (i in deviceList) {
                    deviceListTotleTime += deviceList[i].totleTime
            }
            for (i in planList) {
                    planListTotleTime += planList[i].totleTime
            }
            for (i in changeList) {
                    changeListTime += changeList[i].totleTime
            }
            for (i in qulityList) {
                    qulityListTime += qulityList[i].totleTime
            }
            for (i in changeKnifeList) {
                    changeKnifeListTime += changeKnifeList[i].totleTime
            }
            for (i in matterList) {
                    matterListTime += matterList[i].totleTime
            }
            const productList = [
                { index: '0', category: '设备故障', list: deviceList, sum: deviceList.length, totleTime: deviceListTotleTime },
                { index: '1', category: '计划停机', list: planList, sum: planList.length, totleTime: planListTotleTime },
                { index: '2', category: '更换工装', list: changeList, sum: changeList.length, totleTime: changeListTime },
                { index: '3', category: '质量调整', list: qulityList, sum: qulityList.length, totleTime: qulityListTime },
                { index: '4', category: '更换刀具', list: changeKnifeList, sum: changeKnifeList.length, totleTime: changeKnifeListTime },
                { index: '5', category: '物料等待', list: matterList, sum: matterList.length, totleTime: matterListTime }
            ];
            res.status(200).json(productList)
        }).catch((err) => {
            console.log(err);
        });
    } catch (err) {
        next(err)
    }
}